package cn.itguy.mobileguard.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.SlidingDrawer;

public class WrapSlidingDrawer extends SlidingDrawer {
	
    private boolean mVertical;
    private int mTopOffset;
    
//    private int handleSrcWidth;
    
//    private OnDrawerOpenListener drawerOpenListener = new OnDrawerOpenListener() {
//		
//		@Override
//		public void onDrawerOpened() {
//			ViewGroup.LayoutParams params = getHandle().getLayoutParams();
//            params.height = 0;
//            getHandle().setLayoutParams(params);
//		}
//	};
//	
//	private OnDrawerCloseListener drawerCloseListener = new OnDrawerCloseListener() {
//		
//		@Override
//		public void onDrawerClosed() {
//			ViewGroup.LayoutParams params = getHandle().getLayoutParams();
//            params.height = handleSrcWidth;
//            getHandle().setLayoutParams(params);
//		}
//	};
    
    public WrapSlidingDrawer(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init(attrs);
    }

	public WrapSlidingDrawer(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(attrs);
    }
	
	private void init(AttributeSet attrs) {
		int orientation = attrs.getAttributeIntValue("android", "orientation", ORIENTATION_VERTICAL);
        mTopOffset = attrs.getAttributeIntValue("android", "topOffset", 0);
        mVertical = (orientation == SlidingDrawer.ORIENTATION_VERTICAL);
        
//        setOnDrawerCloseListener(drawerCloseListener);
//        setOnDrawerOpenListener(drawerOpenListener);
	}

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);
        int widthSpecSize =  MeasureSpec.getSize(widthMeasureSpec);
        int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);
        int heightSpecSize =  MeasureSpec.getSize(heightMeasureSpec);

        final View handle = getHandle();
        final View content = getContent();
        measureChild(handle, widthMeasureSpec, heightMeasureSpec);

        if (mVertical) {
            int height = heightSpecSize - handle.getMeasuredHeight() - mTopOffset;
            content.measure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(height, heightSpecMode));
            heightSpecSize = handle.getMeasuredHeight() + mTopOffset + content.getMeasuredHeight();
            widthSpecSize = content.getMeasuredWidth();
            if (handle.getMeasuredWidth() > widthSpecSize) widthSpecSize = handle.getMeasuredWidth();
        }
        else {
            int width = widthSpecSize - handle.getMeasuredWidth() - mTopOffset;
            getContent().measure(MeasureSpec.makeMeasureSpec(width, widthSpecMode), heightMeasureSpec);
            widthSpecSize = handle.getMeasuredWidth() + mTopOffset + content.getMeasuredWidth();
            heightSpecSize = content.getMeasuredHeight();
            if (handle.getMeasuredHeight() > heightSpecSize) heightSpecSize = handle.getMeasuredHeight();
        }

        setMeasuredDimension(widthSpecSize, heightSpecSize);
    }
	
    /**
     * override this method to set the handle align the right side of screen.
     */
    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
    	super.onLayout(changed, l, t, r, b);
    	
        final int width = r - l;
        final int height = b - t;
        
        final View handle = getHandle();

//        handleSrcWidth = handle.getMeasuredWidth();
        
        int childWidth = handle.getMeasuredWidth();
        int childHeight = handle.getMeasuredHeight();

        int childLeft;
        int childTop;

        if (mVertical) {
            childLeft = (width - childWidth) / 2;
            childTop = isOpened() ? mTopOffset : height - childHeight;
        } else {
            childLeft = isOpened() ? mTopOffset : width - childWidth;
            childTop = (height - childHeight) / 2;
        }

        handle.layout(childLeft, childTop, childLeft + childWidth, childTop + childHeight);
    }
    
}